package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.ExchangePriceSetting;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.hdw.CabinetMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.ExchangePriceSettingMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 换电价格设置业务
 * 
 * @Date:Created in 2018/7/9 14:03
 * @Author:
 * @Modified By: zjn 2018/7/9 14:03
 */ 
@Service
public class ExchangePriceSettingService {
    @Autowired
    ExchangePriceSettingMapper exchangePriceSettingMapper;
    @Autowired
    CabinetMapper cabinetMapper;
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/9  14:04
    */ 
    public Page findPage(ExchangePriceSetting search) {
        Page page = search.buildPage();
        page.setTotalItems(exchangePriceSettingMapper.findPageCounts(search));
        search.setBeginIndex(page.getOffset());
        page.setResult(exchangePriceSettingMapper.findPageResults(search));
        return page;
    }
    /**
    * 插入
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  14:04
    */ 
    public ExtResult insert(ExchangePriceSetting entity) {
        //int total = exchangePriceSettingMapper.insert(entity);
        // 修改使用插入方法
        int total = exchangePriceSettingMapper.insertAgency(entity);
        if (total == 0) {
            return ExtResult.failResult("操作失败！");
        }
        return ExtResult.successResult();
    }

    public ExchangePriceSetting find(Long id) {
        return exchangePriceSettingMapper.find(id);
    }
    /**
    * 新增：带代理商查询
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.hdw.ExchangePriceSetting
    * @Author By: zjn 2018/8/3  15:03
    */ 
    public ExchangePriceSetting findAgency(Long id) {
        return exchangePriceSettingMapper.findAgency(id);
    }
    /**
    * 判断套餐是否与换电柜相关联后在删除
    * 
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  14:04
    */ 
    public ExtResult delete(Long id) {
        // 查询主键id
        String cabinetName = cabinetMapper.hasRecordByProperty("priceSettingId", id);
        if (StringUtils.isNotEmpty(cabinetName)) {
            return ExtResult.failResult(String.format("套餐与%s换电柜关联", cabinetName));
        }
        int total = exchangePriceSettingMapper.delete(id);
        if (total == 0) {
            return ExtResult.failResult("操作失败！");
        }
        return ExtResult.successResult();
    }

    /**
    * 更新
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  14:07
    */ 
    public ExtResult update(ExchangePriceSetting entity) {
        int total = exchangePriceSettingMapper.updateAgency(entity);
        if (total == 0) {
            return ExtResult.failResult("操作失败！");
        }
        return ExtResult.successResult();
    }
}
